﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace WeavelWreckingBug
{
    class player : Sprite
    {
        const string playerAssetName = "images\\fancySpid";
        const int position_x = 200;
        const int position_y = 200;
        const int moveSpeed = 160;
        const int moveUp = -1;
        const int moveDown = 1;
        const int moveLeft = -1;
        const int moveRight = 1;

        enum State { walking, jumping }

        State currentState = State.walking;
        Vector2 direction = Vector2.Zero;
        Vector2 speed = Vector2.Zero;
        KeyboardState previousKeyboardState;
        Vector2 startingPosition = Vector2.Zero;

        public void LoadContent(ContentManager contentManager)
        {
            Position = new Vector2(position_x, position_y);
            base.LoadContent(contentManager, playerAssetName);
        }

        public void update(GameTime gameTime)
        {
            KeyboardState currentKeyboardState = Keyboard.GetState();

            UpdateMovement(currentKeyboardState);
            UpdateJump(currentKeyboardState);
            previousKeyboardState = currentKeyboardState;

            base.update(gameTime, speed, direction);
        }

        private void UpdateMovement(KeyboardState currentKeyboardState)
        {
            if (currentState == State.walking)
            {
                speed = Vector2.Zero;
                direction = Vector2.Zero;

                if (currentKeyboardState.IsKeyDown(Keys.Left) == true)
                {
                    speed.X = moveSpeed;
                    direction.X = moveLeft;
                }
                else if (currentKeyboardState.IsKeyDown(Keys.Right) == true)
                {
                    speed.X = moveSpeed;
                    direction.X = moveRight;
                }

                if (currentKeyboardState.IsKeyDown(Keys.Up) == true)
                {
                    speed.Y = moveSpeed;
                    direction.Y = moveUp;
                }
                else if (currentKeyboardState.IsKeyDown(Keys.Down))
                {
                    speed.Y = moveSpeed;
                    direction.Y = moveDown;
                }
            }
        }

        private void UpdateJump(KeyboardState currentKeyboardState)
        {
            if (currentState == State.walking)
            {
                if (currentKeyboardState.IsKeyDown(Keys.Space) == true && previousKeyboardState.IsKeyDown(Keys.Space) == false)
                {
                    Jump();
                }
            }

            if (currentState == State.jumping)
            {
                if (startingPosition.Y - Position.Y > 150)
                {
                    direction.Y = moveDown;
                }

                if (Position.Y > startingPosition.Y)
                {
                    Position.Y = startingPosition.Y;
                    currentState = State.walking;
                    direction = Vector2.Zero;
                }
            }

        }

        private void Jump()
        {
            if (currentState != State.jumping)
            {
                currentState = State.jumping;
                startingPosition = Position;
                direction.Y = moveUp;
                speed = new Vector2(moveSpeed, moveSpeed);
            }
        }
    }
}
